package datamining.visualisation;

import java.awt.BorderLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Properties;
import java.util.Vector;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumnModel;

import datamining.sequence.HPSequence;

/** 
 * <p>
 * 创建于 2012-12-1 下午8:36:14
 * 
 * @author xujinghu (jinghuxu@nudt.edu.cn)
 * <br>
 * Copyright 2012 xujinghu
 * <br>
 * Lisence: Apache
 */
public class SequenceViewTab extends JPanel implements
PropertyChangeListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JTable table;
	private SequenceTableModel model = null;
	private ArrayList<HPSequence> sequencesHP;
	
	public SequenceViewTab(final int index, final Properties p){
		
		JScrollPane scrollPane = new JScrollPane();
		add(scrollPane, BorderLayout.CENTER);
		
		model = new SequenceTableModel(20);
		table = new JTable(model);
		scrollPane.setViewportView(table);
		
		TableColumnModel tcm = table.getColumnModel();
		tcm.getColumn(0).setPreferredWidth(50);
		tcm.getColumn(1).setPreferredWidth(100);
		tcm.getColumn(2).setPreferredWidth(50);
	}
	
	public void setSequences(ArrayList<HPSequence> sequencesHP){
		this.sequencesHP = sequencesHP;
		table.setModel(new SequenceTableModel(sequencesHP));
	}
	
	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		// TODO Auto-generated method stub
		
	}

}

class SequenceTableModel extends AbstractTableModel {

	/**
	 * 用于展示的序列数据
	 */
	private static final long serialVersionUID = 1L;
	@SuppressWarnings("rawtypes")
	private Vector content = null;
	private String[] title_name = { "ID", "序列", "支持度"};
	
	
	@SuppressWarnings("rawtypes")
	public SequenceTableModel(ArrayList<HPSequence> sequencesHP){
		content = new Vector();
		for(Iterator<HPSequence> it = sequencesHP.iterator(); it.hasNext(); ){
			addRow(it.next(),0);
		}
	}
	
	@SuppressWarnings("rawtypes")
	public SequenceTableModel(int count){
		content = new Vector(count);
	}
	
	
	
	public void addRow(HPSequence sequence, int  support) {
		Vector v = new Vector(3);
		v.add(0, new Integer(content.size()));
		v.add(1, sequence);
		v.add(2, new Integer(support));
		content.add(v);
	}
	
	/**
	 * 返回数据类型
	 */
	public Class getColumnClass(int col) {
		return getValueAt(0, col).getClass();
	}

	@Override
	public int getColumnCount() {
		return title_name.length;
	}

	public String getColumnName(int col) {
		return title_name[col];
	}
	
	@Override
	public int getRowCount() {
		return content.size();
	}
	
	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		return ((Vector) content.get(rowIndex)).get(columnIndex);
	}

	/**
	 * 让表格中某些值可修改，但需要setValueAt(Object value, int row, int col)方法配合才能使修改生效
	 */
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		if (columnIndex == 0) {
			return false;
		}else{
			return true;
		}
	}

	public void removeRow(int row) {
		content.remove(row);
	}

	public void removeRows(int row, int count) {
		for (int i = 0; i < count; i++) {
			if (content.size() > row) {
				content.remove(row);
			}
		}
	}

	/**
	 * 使修改的内容生效
	 */
	public void setValueAt(Object value, int row, int col) {
		((Vector) content.get(row)).remove(col);
		((Vector) content.get(row)).add(col, value);
		this.fireTableCellUpdated(row, col);
	}
	
}